package com.qingfeng.manager.config.shiro;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.qingfeng.core.entity.Permission;
import com.qingfeng.manager.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description: url filter
 * @author:LWY
 * @date: 2017/11/4 15:12
 * @modified:
 */
@Service
public class UrlFilter {

    @Autowired
    PermissionService permissionService;

    /**
     * 加载权限
     *
     * @throws Exception
     */
    public void loadPermision(ShiroFilterFactoryBean shiroFilterFactoryBean) throws Exception {
        AbstractShiroFilter shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean.getObject();
        // 获取过滤管理器
        PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver();
        DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver.getFilterChainManager();
        // 清空初始权限配置
        manager.getFilterChains().clear();
        //加载权限
        shiroFilterFactoryBean.setFilterChainDefinitionMap(urlFileter(false));
        Map<String, String> filterChainDefinitionMap = shiroFilterFactoryBean.getFilterChainDefinitionMap();
        for (Map.Entry<String, String> entry : filterChainDefinitionMap.entrySet()) {
            String url = entry.getKey();
            String chainDefinition = entry.getValue().trim().replace(" ", "");
            if (StringUtils.isNoneEmpty(url)) {
                manager.createChain(url, chainDefinition);
            }
        }
    }


    /**
     * @param init 是否初次加载（启动时加载）
     * @return
     */
    public Map<String, String> urlFileter(boolean init) {
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/page/lib/**", "anon");
        filterChainDefinitionMap.put("/page/static/**", "anon");
        filterChainDefinitionMap.put("/page/temp/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        //登录不被拦截
        filterChainDefinitionMap.put("/login", "anon");
        //验证码不被拦截
        filterChainDefinitionMap.put("/veriCode", "anon");
        //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了
        filterChainDefinitionMap.put("/logout", "logout");
        if (!init) {
            //<!-- 过滤链定义，从上向下顺序执行，一般将/**放在最为下边 -->:这是一个坑呢，一不小心代码就不好使了;
            PageInfo<Permission> all = permissionService.get();
            for (Permission permision : all.getList()) {
                filterChainDefinitionMap.put(permision.getUrl(), "perms[" + permision.getId() + "]");
            }
        }
        //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
        filterChainDefinitionMap.put("/**", "authc");
        return filterChainDefinitionMap;
    }
}
